package com.bw.home.view

import android.util.Log
import androidx.fragment.app.activityViewModels
import com.alibaba.android.arouter.launcher.ARouter
import com.baway.home.adapter.HomeBannerAdapter
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.common.entity.BannerEntity
import com.bw.common.view.BaseMvvmFragment
import com.bw.home.R
import com.bw.home.adapter.HomeAdapter
import com.bw.home.adapter.HomeTypeAdapter
import com.bw.home.databinding.FragmentHomeBinding
import com.bw.home.vm.HomeViewModel
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class HomeFragment : BaseMvvmFragment<FragmentHomeBinding>() {

    val homeList = listOf(
        HomeEntity(1,"车位管理", R.drawable.img_3),
        HomeEntity(2,"访客登记", R.drawable.img_11),
        HomeEntity(3,"物业保修", R.drawable.img_5),
        HomeEntity(4,"考勤打卡", R.drawable.img_6),
        HomeEntity(5,"房源管理", R.drawable.img_7),
        HomeEntity(6,"商家推荐", R.drawable.img_8),
        HomeEntity(7,"月卡会员", R.drawable.img_9),
        HomeEntity(8,"设备管理", R.drawable.img_10),
    )

    private val bannerData = mutableListOf(
        BannerEntity(0, R.drawable.home_banner_0, ""),
        BannerEntity(1, R.drawable.home_banner_1, ""),
        BannerEntity(2, R.drawable.home_banner_2, ""),
        BannerEntity(2, R.drawable.ic_banner, ""),
    )
    override fun getLayoutId(): Int = R.layout.fragment_home


    val homeAdapter = HomeAdapter()
    val homeTypeAdapter = HomeTypeAdapter()

    // 在Fragment中使用activityViewModels()函数，在Activity中，使用viewModels()函数
    private val mViewModel:HomeViewModel by activityViewModels()
    private var page = 1
    private var size = 10
    override fun initView() {
        //设置banner
        mDataBinding.bannerV2.run {
            setAdapter(HomeBannerAdapter(bannerData))
            setPageTransformer(AlphaPageTransformer())
            setIndicator(CircleIndicator(this@HomeFragment.requireContext()))
            addBannerLifecycleObserver(this@HomeFragment.viewLifecycleOwner) //关联Fragment生命周期
        }
        mDataBinding.homeRv.adapter = homeTypeAdapter
        homeTypeAdapter.setList(homeList)


        //公告列表
        mDataBinding.rv.adapter = homeAdapter
        mViewModel.mSuccess.observe(this){
            homeAdapter.data.addAll(it)
            homeAdapter.notifyDataSetChanged()
        }



        //跳转到
        homeTypeAdapter.setOnItemClickListener { adapter, view, position ->
            val item = homeTypeAdapter.data[position]
            when(item.id){
                1->{}
                2->{}
                3->{}
                4->{}
                5->{}
                6->{
                    ToastUtils.showLong(item.id)
                    ARouter.getInstance().build(RouterPath.PATH_MERCHANT_PATH).navigation()
                }
                7->{}
                8->{}
            }
        }


    }

    override fun loadData() {
        super.loadData()
        mViewModel.getListData(1,10)
    }

    override fun observeDataChanged() {
        super.observeDataChanged()
        mViewModel.mSuccess.observe(this@HomeFragment.requireActivity()){
            Log.e("homePage", "====>: ${GsonUtils.toJson(it)}" )
        }
    }
}


data class HomeEntity(
    val id:Int,
    val title:String,//标题
    val image:Int,//图片
)